<template>
  <div class="admin-login">
    <!-- 注册/登录表单 -->
    <el-card class="form-card">
      <div class="form-header">
        <img src="@/assets/logo.png" alt="Logo" class="logo" />
      </div>

      <!-- 登录表单 -->
      <el-form v-if="activeForm === 'login'" :model="loginForm" :rules="loginRules" ref="loginFormRef">
        <el-form-item prop="email">
          <el-input
              v-model="loginForm.userName"
              placeholder="用户名"
              :prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              :prefix-icon="Lock"
              show-password
          />
        </el-form-item>
        <el-button type="primary" class="submit-btn" @click="handleLogin">登录</el-button>

      </el-form>



    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { User, Lock,} from '@element-plus/icons-vue'
import {ElMessage} from "element-plus";
import {adminLoginService} from "@/api/admin";

const loginFormRef = ref(null)
const loginForm = ref({
  userName: '',
  password: ''
});
const activeForm = ref('login');

// 登录表单验证规则
const loginRules = {
  userName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
};
// 通用表单验证方法
const validateForm = async (formRef) => {
  if (!formRef.value) return false
  try {
    await formRef.value.validate()
    return true
  } catch (error) {
    return false
  }
}
const handleLogin = async () => {
  if (!await validateForm(loginFormRef)) {
    ElMessage.warning('请正确填写所有必填项')
    return
  }

  try {

    const {code,message,data } = await adminLoginService(loginForm)

    if (code === 200) {
      ElMessage.success('登录成功')
      // TODO: 处理登录成功后的逻辑
    } else {
      console.log(data)
      ElMessage.error(message || '登录失败')
    }
  } catch (error) {
    // ElMessage.error('登录失败，请稍后重试')
  }

};
</script>


<style scoped>
.admin-login {
  max-width: 400px;
  margin: 40px auto;
  padding: 20px;
  position: relative;
  z-index: 1;
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
}

.form-card {
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 30px;
  transition: transform 0.3s ease;
  cursor: default;
}

.form-card:hover {
  transform: translateY(-2px);
}

.form-header {
  text-align: center;
  margin-bottom: 30px;
  user-select: none;
}

.logo {
  width: 80px;
  height: 80px;
  margin-bottom: 20px;
  transition: transform 0.3s ease;
}

.logo:hover {
  transform: scale(1.05);
}

h2 {
  margin: 0;
  font-size: 24px;
  color: #202124;
  font-weight: 400;
  user-select: none;
}


.submit-btn {
  width: 100%;
  margin-top: 20px;
  height: 40px;
  font-size: 16px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}


:deep(.el-input__wrapper) {
  padding: 0 15px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  transition: all 0.3s ease;
  cursor: text;
}

:deep(.el-input__wrapper:hover) {
  background: rgba(255, 255, 255, 1);
}

:deep(.el-input__inner) {
  height: 40px;
}

:deep(.el-form-item__error) {
  padding-top: 4px;
  user-select: none;
}


:deep(.el-button--link) {
  transition: all 0.3s ease;
}

:deep(.el-button--link:hover) {
  text-decoration: none;
  transform: translateY(-1px);
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.form-card {
  animation: fadeIn 0.5s ease-out;
}

/* 允许输入框文本选择 */
:deep(.el-input__inner) {
  user-select: text !important;
  -webkit-user-select: text !important;
  -moz-user-select: text !important;
  -ms-user-select: text !important;
}

/* 确保按钮和链接有正确的光标 */
:deep(.el-button) {
  cursor: pointer !important;
}

/* 步骤条样式优化 */
:deep(.el-steps) {
  margin-bottom: 30px;
}

:deep(.el-step__title) {
  font-size: 14px;
  color: #5f6368;
}

:deep(.el-step__title.is-success) {
  color: #67c23a;
}

:deep(.el-step__title.is-process) {
  color: #409eff;
  font-weight: 500;
}

/* 表单项间距优化 */
:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-form-item:last-child) {
  margin-bottom: 0;
}
</style>